package com.datastructure.test.findlongestsubarray;

import java.util.ArrayList;

public class FindLongestSubArray {

    public static void main(String[] args) {

    }

    public int findLongestSubArray (ArrayList<Integer> nums) {
        int n = nums.size();
        int[][] dp = new int[n][2];
        if (nums.get(0) > 0) dp[0][0] = 1;
        if (nums.get(0) < 0) dp[0][1] = 1;
        int res = dp[0][0];
        for (int i = 1; i < n; i++) {
            if (nums.get(i) > 0) {
                dp[i][0] = Math.max(dp[i][0], dp[i-1][0]+1);
                if (dp[i-1][1] > 0) {
                    dp[i][1] = Math.max(dp[i][1], dp[i-1][1]+1);
                }
            } else if (nums.get(i) < 0) {
                dp[i][1] = Math.max(dp[i][1], dp[i-1][0]+1);
                if (dp[i-1][1] > 0) {
                    dp[i][0] = Math.max(dp[i][0], dp[i-1][1]+1);
                }
            }
            res = Math.max(res, dp[i][0]);
        }
        return res;
    }
}
